package com.cj.Interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * Created by chenjie on 2017/5/7.
 */
public class BaseInterceptor implements HandlerInterceptor {
    private static Logger logger = LoggerFactory.getLogger(BaseInterceptor.class);

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {

//        Long expire = redisTemplate.getExpire("7eea37b5-061e-4a99-a74f-c224c09fd1a1");
        Cookie[] cookies = request.getCookies();
        HttpSession session = request.getSession(false);


        if(session==null){
            response.getWriter().print("no");
//            response.sendRedirect("http://localhost/login.html");
            return false;
        }
        String loginname = String.valueOf(session.getAttribute("loginname"));
        if(loginname ==null || loginname==""){
            response.getWriter().print("no");
//            response.sendRedirect("http://localhost/login.html");
            return false;
        }
        logger.info("session"+session.getId());
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws Exception {

    }
}
